/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package publicRelations.beans;

import facades.CandidateEconomicTypeFacade;
import facades.CandidateFacade;
import facades.CandidateTypeFacade;
import facades.UserFacade;
import facades.UserTypeFacade;
import entities.Candidate;
import entities.User;
import util.ImageStoreHelper;
import util.Sha256Encrypter;
import java.io.File;
import java.io.IOException;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import entities.PublicRelations;
import entities.PublicRelationsCandidate;
import facades.PublicRelationsCandidateFacade;
import facades.PublicRelationsFacade;
import qualifiers.CreatedEntityEvent;
import qualifiers.EditedEntityEvent;
import qualifiers.PreEditEntityEvent;

/**
 *
 * @author damfercer
 */
@Named(value = "createCandidateForPublicRelationsBean")
@SessionScoped
public class CreateCandidateForPublicRelationsBean implements Serializable {
    @Inject
    @PreEditEntityEvent
    private Event<PublicRelations> preEditPublicRelationsEvent;
    @Inject
    @EditedEntityEvent
    private Event<PublicRelations> editedPublicRelationsEvent;
    @Inject
    UserFacade userFacade;
    @Inject
    PublicRelationsFacade publicRelationsFacade;
    @Inject
    PublicRelationsCandidateFacade publicRelationsCandidateFacade;
    @Inject
    CandidateFacade candidateFacade;
    @Inject
    UserTypeFacade userTypeFacade;
    @Inject
    CandidateEconomicTypeFacade candidateEconomicTypeFacade;
    @Inject
    CandidateTypeFacade candidateTypeFacade;
    @Inject
    @CreatedEntityEvent
    private Event<Candidate> newCandidateEvent;
    @Inject
    @CreatedEntityEvent
    private Event<User> newUserEvent;
    private String publicRelationsId;
    private PublicRelations publicRelation;
    private Candidate candidate;

    /**
     * Creates a new instance of CreateCandidateForCompanyBean
     */
    public CreateCandidateForPublicRelationsBean() {
    }
    


    
    public String retrievePublicRelations(){
        if(publicRelationsId!=null){
            Integer idPublicRelations;
            PublicRelations pR;
            try{
                idPublicRelations=new Integer(publicRelationsId);
                pR=publicRelationsFacade.find(idPublicRelations);
            }
            catch(Exception e){
                pR=null;
            }
            if(pR!=null){
                publicRelation=pR;
                publicRelation.setReferals(publicRelation.getReferals()+1);
                preEditPublicRelationsEvent.fire(publicRelation);
                publicRelationsFacade.edit(publicRelation);
                editedPublicRelationsEvent.fire(publicRelation);
                if(candidate==null)
                candidate=new Candidate();
                if(candidate.getUser()==null)
                candidate.setUser(new User());
                getCandidate().getUser().setUserType(userTypeFacade.find(1));
                candidate.getUser().setRelatedUser(pR.getUser());
                return null;
            }
            else{
                return "/index.xhtml";  
            }
        }
        else{
            return "/index.xhtml";
        }
    }
    
    public String createCandidate() throws IOException{
        persistCandidate();
        makeDirs();
        PublicRelationsCandidate publicRelationsCandidate=new PublicRelationsCandidate();
        publicRelationsCandidate.setAmount(publicRelation.getPayPerCandidate());
        publicRelationsCandidate.setCandidate(candidate);
        //profile.setFolder(null);
        publicRelationsCandidateFacade.create(publicRelationsCandidate);
        if(candidate.getPublicRelationsCandidate()==null){
            candidate.setPublicRelationsCandidate(publicRelationsCandidate);
        }
        if(publicRelation.getUser().getUserCollection()==null){
            publicRelation.getUser().setUserCollection(new ArrayList<User>());
        }
        //candidate.getCompanyAdquiredProfileList().add(profile);
        publicRelation.getUser().getUserCollection().add(candidate.getUser());
        return "/views/candidate/NewEditCandidateView?faces-redirect=true";
    }
    
    public void makeDirs(){
        File imagesDir=new File(ImageStoreHelper.pathToPersistentStore+candidate.getIdCandidate().toString()+"/"+"images");
        File videosDir=new File(ImageStoreHelper.pathToPersistentStore+candidate.getIdCandidate().toString()+"/"+"videos");
        imagesDir.mkdirs();
        videosDir.mkdirs();
    }
    
    private void persistCandidate() throws IOException{
        Date date = new Date();
        candidate.getUser().setCreationDate(date);
        candidate.getUser().setLastDate(date);
        candidate.getUser().setLogins(0);
        candidate.getUser().setActive(true);
        candidate.setLastModificationDate(date);
        candidate.setVisits(0);
        candidate.setCandidateEconomicType(candidateEconomicTypeFacade.find(1));
        candidate.setCandidateType(candidateTypeFacade.find(1));
        //String password=candidate.getUser().getPassword();
        try {
            candidate.getUser().setEmail(candidate.getUser().getEmail().toLowerCase());
            candidate.getUser().setPassword(Sha256Encrypter.getSha256(getCandidate().getUser().getPassword()));
        } catch (NoSuchAlgorithmException ex) {
//            Logger.getLogger(CreateCandidateBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
//            Logger.getLogger(CreateCandidateBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        candidate.getUser().setCandidate(candidate);
        userFacade.create(candidate.getUser());
        //candidateFacade.create(candidate);
        //candidate.getUser().setCandidate(getCandidate());
        newUserEvent.fire(candidate.getUser());
        //candidateFacade.edit(candidate);
        newCandidateEvent.fire(candidate);
        //candidate.getUser().setPassword(password);
    }


    /**
     * @return the candidate
     */
    public Candidate getCandidate() {
        return candidate;
    }

    /**
     * @param candidate the candidate to set
     */
    public void setCandidate(Candidate candidate) {
        this.candidate = candidate;
    }

    /**
     * @return the publicRelation
     */
    public PublicRelations getPublicRelation() {
        return publicRelation;
    }

    /**
     * @param publicRelation the publicRelation to set
     */
    public void setPublicRelation(PublicRelations publicRelation) {
        this.publicRelation = publicRelation;
    }

    /**
     * @return the publicRelationsId
     */
    public String getPublicRelationsId() {
        return publicRelationsId;
    }

    /**
     * @param publicRelationsId the publicRelationsId to set
     */
    public void setPublicRelationsId(String publicRelationsId) {
        this.publicRelationsId = publicRelationsId;
    }
}
